package is.web;

import is.interfaces.Bean;
import java.util.LinkedList;

/**
 *
 * @author aspr0
 */
public class Form
{
    public static String generate(Bean bean, LinkedList<String> selects, Integer... selectFields)
    {
        String formCanvas =
                "<form method=\"post\" enctype=\"\">"
                + "<table>"
                + "%s"
                + "</table><input type=\"submit\"></form>";

        String rowCanvas =
                "<tr>"
                + "<td>%s</td>"
                + "<td>%s</td>"
                + "</tr>";

        String inputCanvas = "<input type=\"text\" name=\"%s\" value=\"%s\" class=\"wide\">";

        String rows =
                "<tr>"
                + "<td>id</td>"
                + "<td>"+bean.getId()+"</td>"
                + "</tr>";

        for (int partNum = 0; partNum < bean.getFields().length; partNum++)
        {
            Boolean matchSelect = false;

            for (int selectField : selectFields)
            {
                if (partNum == selectField)
                {
                    matchSelect = true;
                    break;
                }
            }

            String input;

            if (matchSelect)
            {
                input = selects.removeFirst();
            }
            else
            {
                input = String.format(inputCanvas, bean.getFields()[partNum], bean.getValues()[partNum]);
            }
            rows += String.format(rowCanvas, bean.getFields()[partNum], input);
        }

        return String.format(formCanvas, rows);
    }
}
